<!DOCTYPE html>
<html class="ui-page-login">

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<link href="css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" type="text/css" href="css/beiyong.css"/>
		<link rel="stylesheet" type="text/css" href="css/general.css"/>
		<style>
			.ui-page-login,
			body {
				width: 100%;
				height: 100%;
				margin: 0px;
				padding: 0px;
			}
			.mui-content{height: 100%; padding:0 20px; background:url(images/logoBg.png); background-size:100% 100%;}
			.area {
				margin:20px auto 0px auto;
			}
			
			.mui-input-group {
				margin-top: 10px;
			}
			
			.mui-input-group:first-child {
				margin-top: 20px;
			}
			
			/*.mui-input-group label {
				width: 22%;
			}
			
			.mui-input-row label~input,
			.mui-input-row label~select,
			.mui-input-row label~textarea {
				width: 78%;
			}*/
			
			.mui-checkbox input[type=checkbox],
			.mui-radio input[type=radio] {
				top: 6px;
			}
			
			.mui-content-padded {
				margin-top: 25px 10px;
			}
			
			.mui-btn {
				padding: 10px;
			}
			
			.link-area {
				display: block;
				margin-top:15px;
				text-align: center;
				font-size: 15px;
			}
			.link-area a{ color:#444;}
			.spliter {
				color: #bbb;
				padding: 0px 8px;
				clear:both;
			}
			
			.oauth-area {
				position: absolute;
				bottom: 50px;
				left: 0px;
				text-align: center;
				width: 100%;
				padding: 0px;
				margin: 0px;
			}
			
			.oauth-area .oauth-btn {
				display: inline-block;
				width: 50px;
				height: 50px;
				background-size: 30px 30px;
				background-position: center center;
				background-repeat: no-repeat;
				margin: 0px 20px;
				/*-webkit-filter: grayscale(100%); */
				border: solid 1px #ddd;
				border-radius: 25px;
			}
			
			.oauth-area .oauth-btn:active {
				border: solid 1px #aaa;
			}
			
			.oauth-area .oauth-btn.disabled {
				background-color: #ddd;
			}
			.sf{
				width: 100%;
				display: flex;
				align-items: center;
				justify-content: center;
				
			}
			.qq{
				width: 25%;
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;
			}
			.img{
				width: 50px;
				height: 50px;
				margin:.5em 0;
			}
			.img img{
				width: 100%;
				height: 100%;
				
			}
			.dsf{
				width: 100%;
				text-align: center;
				position:relative;
				/*margin-bottom:.6em;*/
				height:2em;
					
			}
			.dsf>span{	background:#E7EFFE; position:absolute; left:50%; top:50%;  transform:translate(-50%,-50%); padding:0 15px; }
			
			.list_item{ background:transparent; }
			.items{ height:50px!important; position:relative;  color:#333;    display: flex;  align-items: center;
                  justify-content: flex-start;
				  
				margin:16px 10px 0;
				border-bottom:1px solid #fff;
				/*background: #fff !important;*/
				border-radius: 3px;
				font-size: 15px;
			
			}
			
			
				  
			.items:after{left:0!important; background:#fff!important;}
			.list_item:after,.list_item:before{ background:transparent;}
			#login{
				
				background:#7DB1FD; border:0;
				margin-top:50px;
				border-radius: 30px;
			
			}
			.mui-input-row .mui-input-clear~.mui-icon-clear{ top:20px;}
			.dsf:before{ height: 1px;   content: ''; position:absolute; left:50%; top:50%;  transform:translate(-50%,-50%);   width:65%; background:#666;}
			.dsfdiv{
				position: absolute;
				width: 100%;
				bottom: 1em;
			}
			.logo{ width:130px; height:120px; margin:15px auto;}
			.logo img{ width:100%; height:100%; display: block;}
			#reg{ float:left;}
			#reg span{ font-family: '宋体'; padding-left:2px;}
			#forgetPassword{ float:right;}
			input::-webkit-input-placeholder {
		       /* placeholder颜色  */
		        color: #444;
		     
		     }
		
		</style>

	</head>

	<body>
		<header class="mui-bar mui-bar-nav hbg">
			<h1 class="mui-title">渭书</h1>
		</header>
		<div class="mui-content">
			<div class="logo"> <img src="./images/logo.png" alt="" /></div>
			<form id='login-form' class="list_item mui-input-group">
				<div class="mui-input-row items">
					<div> <i class="mui-icon mui-icon-phone"></i></div>
					<input id='account' type="text" value="" class="mui-input-clear mui-input" placeholder="请输入账号">
				</div>
				<div class="mui-input-row items">
					<div><i class="mui-icon mui-icon-locked"></i></div>
					<input id='password' type="password" value="" class="mui-input-clear mui-input" placeholder="请输入密码">
				</div>
			</form>
			<div class="mui-content-padded">
				<div class="link-area"><a id='reg'>没有账号注册账号<span>></span></a> <span class="spliter"></span> <a id='forgetPassword'>忘记密码?</a>
				
				
				</div>
				<button id='login' class="mui-btn mui-btn-block mui-btn-primary">登录</button>
			</div>
			<div class="mui-content-padded oauth-area">

			</div>
		</div>
		<script src="dynamic/js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/mui.min.js"></script>
		<script src="js/muishow.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/req.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/mui.enterfocus.js"></script>
		<script src="js/app.js"></script>
		<script>
			(function($, doc) {
				$.init({
					statusBarBackground: '#f7f7f7',
				});
				
					var regButton = doc.getElementById('reg');
					var forgetButton = doc.getElementById('forgetPassword');
					var login = doc.getElementById('login');
					var accountBox = doc.getElementById('account');
					var passwordBox = doc.getElementById('password');
					var phone = localStorage.getItem("userphone")||"";
					var psword = localStorage.getItem("password")||"";
					console.log(phone);
					console.log(psword);
					if(""!=phone){
						accountBox.value = phone;
						passwordBox.value = psword;
					}
					
					login.addEventListener('tap', function(event) {
					console.log("accountBox",accountBox.value);
					let str = /^[1][3,4,5,7,8][0-9]{9}$/
					//let str = /^/;
					if(!str.test(accountBox.value) || "" == passwordBox.value || null == passwordBox.value){
						mui.toast("账号或密码输入有误");
						return;
					}
					let data = {
						url:"/api/Login/login",
						data:{
							mobile:accountBox.value,
							password:passwordBox.value,
						}
					};
					ajax(data,function(res){
							console.log(res);
							if(res.code == -1){
								mui.toast(res.message);
							}
							if(res.code == 1){
								localStorage.setItem("userphone",accountBox.value);
								localStorage.setItem("password",passwordBox.value);
								console.log(accountBox.value);
								localStorage.setItem("accesstoken",res.data[0].access_token);
								localStorage.setItem("account",res.data[0].user.account);
								localStorage.setItem("headimage",res.data[0].user.headimage);
								localStorage.setItem("id",res.data[0].user.id);
								localStorage.setItem("mobile",res.data[0].user.mobile);
								localStorage.setItem("nickname",res.data[0].user.nickname);
								mui.openWindow({
									url: 'index5.html',
									id: 'index',
								});
								//toMain();
							}
						});
					}, false);
				
					$.enterfocus('#login-form input', function() {
						$.trigger(loginButton, 'tap');
					});
			regButton.addEventListener('tap', function(event) {
						mui.openWindow({
							url: 'reg.html',
							id: 'reg',
							preload: true,
							show: {
								aniShow: 'pop-in'
							},
							styles: {
								popGesture: 'hide'
							},
							waiting: {
								autoShow: false
							}
						});
					}, false);
			
			forgetButton.addEventListener('tap', function(event) {
						mui.openWindow({
							url: 'forget_password.html',
							id: 'forget_password',
							preload: true,
							show: {
								aniShow: 'pop-in'
							},
							styles: {
								popGesture: 'hide'
							},
							waiting: {
								autoShow: false
							}
						});
					}, false);	
					
				$.plusReady(function() {
					plus.screen.lockOrientation("portrait-primary");
					var settings = app.getSettings();
					var state = app.getState();
					/*var mainPage = plus.webview.getWebviewById("index");
					console.log(mainPage);
					var main_loaded_flag = false;
					if(!mainPage){
						mainPage = $.preload({
							"id": 'index',
							"url": 'index4.html'
						});
					}else{
						main_loaded_flag = true;
					}*/
					/*var toMain = function() {
						//使用定时器的原因：
						//可能执行太快，main页面loaded事件尚未触发就执行自定义事件，此时必然会失败
						var id = setInterval(function () {
							if(main_loaded_flag){
								clearInterval(id);
								$.fire(mainPage, 'show', null);
								mainPage.show("pop-in");
							}
						},20);
					};*/
					/*mainPage.addEventListener("loaded",function () {
						main_loaded_flag = true;
					});*/
					
					//检查 "登录状态/锁屏状态" 开始
					if (settings.autoLogin && state.token && settings.gestures) {
						$.openWindow({
							url: 'unlock.html',
							id: 'unlock',
							show: {
								aniShow: 'pop-in'
							},
							waiting: {
								autoShow: false
							}
						});
					} else if (settings.autoLogin && state.token) {
						toMain();
					} else {
						app.setState(null);
						//第三方登录
						//var authBtns = ['qihoo', 'weixin', 'sinaweibo', 'qq']; //配置业务支持的第三方登录
						var authBtns = ['weixin']; //配置业务支持的第三方登录
						var auths = {};
						var oauthArea = doc.querySelector('.oauth-area');
						plus.oauth.getServices(function(services) {
							
							for (var i in services) {
								var service = services[i];
								auths[service.id] = service;
								if (~authBtns.indexOf(service.id)) {
									var isInstalled = app.isInstalled(service.id);
									var btn = document.createElement('div');
									//如果微信未安装，则为不启用状态
									btn.setAttribute('class', 'oauth-btn' + (!isInstalled && service.id === 'weixin' ? (' disabled') : ''));
									btn.authId = service.id;
									
									btn.style.backgroundImage = 'url("images/' + service.id + '.png")'
									oauthArea.appendChild(btn);
								}
							}
							$(oauthArea).on('tap', '.oauth-btn', function() {
								if (this.classList.contains('disabled')) {
									plus.nativeUI.toast('您尚未安装微信客户端');
									return;
								}
								var auth = auths[this.authId];
								var waiting = plus.nativeUI.showWaiting();
								auth.login(function() {
									waiting.close();
									plus.nativeUI.toast("登录认证成功");
									auth.getUserInfo(function() {
										plus.nativeUI.toast("获取用户信息成功");
										//console.log(auth.userInfo);
										
										var name = auth.userInfo.nickname || auth.userInfo.name;
										app.createState(name, function() {
											
											let data = {
												url:"/api/Login/wxlogin",
												data:{
													wx_openid:auth.userInfo.openid
												}
											}
											ajax(data,function(res){
												console.log(res);
												if(res.code == -1){
													mui.toast(res.message);
												}
												if(res.code == 1){
													localStorage.removeItem("accesstoken");
													localStorage.removeItem("account");
													localStorage.removeItem("headimage");
													localStorage.removeItem("id");
													localStorage.removeItem("mobile");
													localStorage.removeItem("nickname");
													localStorage.removeItem("wx_openid");
													localStorage.setItem("accesstoken",res.data[0].access_token);
													localStorage.setItem("account",res.data[0].user.account);
													localStorage.setItem("headimage",res.data[0].user.headimage);
													localStorage.setItem("id",res.data[0].user.id);
													localStorage.setItem("mobile",res.data[0].user.mobile);
													localStorage.setItem("nickname",res.data[0].user.nickname);
													localStorage.setItem("wx_openid",res.data[0].user.wx_openid);
													
													mui.openWindow({
														url: 'index5.html',
														id: 'index',
													});
													
													//toMain();
												}
											})
										});
									}, function(e) {
										plus.nativeUI.toast("获取用户信息失败：" + e.message);
									});
								}, function(e) {
									waiting.close();
									plus.nativeUI.toast("登录认证失败：" + e.message);
								});
							});
						}, function(e) {
							oauthArea.style.display = 'none';
							plus.nativeUI.toast("获取登录认证失败：" + e.message);
						});
					}
					// close splash
					setTimeout(function() {
						//关闭 splash
						plus.navigator.closeSplashscreen();
					}, 600);
					//检查 "登录状态/锁屏状态" 结束
						
					
					window.addEventListener('resize', function() {
						oauthArea.style.display = document.body.clientHeight > 400 ? 'block' : 'none';
					}, false);
					//
					var backButtonPress = 0;
					$.back = function(event) {
						backButtonPress++;
						if (backButtonPress > 1) {
							plus.runtime.quit();
						} else {
							plus.nativeUI.toast('再按一次退出应用');
						}
						setTimeout(function() {
							backButtonPress = 0;
						}, 1000);
						return false;
					};
				});
			}(mui, document));
			
			
			
		</script>
	</body>

</html>